
local locplayer = ac.player.self
local w,h = 420,190
local ui = class.panel:builder 
{
    x = (1920 - w)/2,
    y = 300,
    w = w,
    h = h,
    show_flag = false,
    alpha = 0.99,
    level = 99,
    fdf = 'tooltip_backdrop0',
}
ui:set_level(99)
ui.title = ui:add_text('|cffffff00替换天赋|r',0,5,nil,30,12,5)
ui.esc = ui:add_button('',(ui.w-120)/2,ui.h + 20,120,40,1)
ui.esc:add_frame('tooltip_backdrop0')
ui.esc:add_text('不替换',0,0,nil,nil,12,5)

local normal_icon = [[core/emptyinvslot1.blp]]
local w1,h1 = 64,64
local x1 = (w-w1*6)/2
local y1 = (h-h1*2-ui.title.h)/2
ui.buttons = {}

for i=1,12 do
    local x = (i-1)%6 * (w1+1) + x1
    local y = (math.ceil(i/6)-1) * (h1+1) + 30 + y1
    local button = ui:add_button(normal_icon,x,y,w1,h1)
    button.icon = button:add_panel('',3,3,w1-8,h1-8)
    button.index = i
    
    button.tfcard = button:add_panel([[core/bantouming.blp]],0,0,35,20)
    button.tfcard.text = button.tfcard:add_text('名称',0,0,button.tfcard.w+50,nil,8,4)

    ui.buttons[i] = button
end

ui.select = ui:add_panel('1',0,0,w1,h1)
ui.select:set_alpha(50)
ui.select:hide()


function ui:update()
    local hero = locplayer.hero
    local data = hero and hero:get_data('展示天赋表') or {}
    for i,button in ipairs(self.buttons) do
        local skill = data[i]
        button.skill = skill
        if skill then
            button.icon:set_normal_image(skill.art)
            button.tfcard.text:set_text(skill.show_class)
            button.tfcard:show()
        else
            button.icon:set_normal_image('')
        end
    end
    self.select:hide()
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local x,y = button:get_real_position()
        self.select:set_real_position(x,y)
        self.select:show()
        self:tooltip(skill:get_title(),skill:get_tip())
    end
end

function ui:on_button_mouse_leave(button)
    self.select:hide()
end

function ui:on_button_clicked(button)
    if button==self.esc then
        self:hide()
        return
    end
    ac.sync('确定替换抽卡',button.index)
end

ac.sync:event '确定替换抽卡'(function(_,player,index)
    local hero = player.hero
    local name = player:get_data('替换抽卡名')
    if hero and name then
        player:set_data('替换抽卡名',nil)
        hero:notify('英雄-获得天赋',hero,name,index)
    end
    if player:is_self() then
        ui:hide()
    end
end)

ac.game:event '英雄-替换抽卡'(function(_,hero,name)
    local player = hero.owner
    player:set_data('替换抽卡名',name)
    if player:is_self() then
        ui:update()
        ui:show()
    end
end)


ac.game:event '玩家-刷新UI'(function(_,player)
    if player:is_self() then
        ui:update()
    end
end)

